package com.youkeyi.ddy.cloud.application.system.auth.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.auth.SystemAuthCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.dto.SystemAuthListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.dto.SystemAuthListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.param.SystemAuthCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.param.SystemAuthDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.param.SystemAuthListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.auth.param.SystemAuthUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.PublicAuthCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.dto.PublicAuthListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SystemAuthController
 *
 * @Author liuxiawang
 * @Date 2024/2/27 4:59 PM
 * @Location shenzhen.china
 */
@Service
public class SystemAuthController implements SystemAuthCloudService {

    @Autowired
    private PublicAuthCloudService publicAuthCloudService;

    @Override
    public SystemAuthListCloudDto list(SystemAuthListCloudParam param) {
        SystemAuthListCloudDto dto = new SystemAuthListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicAuthQueryCloudParam authQueryCloudParam = new PublicAuthQueryCloudParam();
        authQueryCloudParam.setAuthPrimaryKeys(param.getAuthPrimaryKeys());
        authQueryCloudParam.setNames(param.getNames());
        authQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        authQueryCloudParam.setCodes(param.getCodes());
        authQueryCloudParam.setAuthTypes(param.getAuthTypes());
        authQueryCloudParam.setUrlFuzzy(param.getUrlFuzzy());
        authQueryCloudParam.setPageSize(param.getPageSize());
        authQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicAuthListCloudDto authDtos = publicAuthCloudService.query(authQueryCloudParam);
        if (null != authDtos && CollectionUtils.isNotEmpty(authDtos.getRows())) {
            List<SystemAuthListItemCloudDto> items = new ArrayList<>();
            authDtos.getRows().forEach(auth -> {
                SystemAuthListItemCloudDto item = new SystemAuthListItemCloudDto();
                item.setAuthPrimaryKey(auth.getAuthPrimaryKey());
                item.setName(auth.getName());
                item.setCode(auth.getCode());
                item.setAuthType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_AUTH_TYPE.getName(auth.getAuthType()));
                item.setAuthTypeName(item.getAuthType());
                item.setUrl(auth.getUrl());
                item.setIsDeleted(auth.getIsDeleted());
                item.setIsDeletedName(auth.getIsDeletedName());
                item.setCreateUserKey(auth.getCreateUserKey());
                item.setCreateTime(auth.getCreateTime());
                item.setUpdateUserKey(auth.getUpdateUserKey());
                item.setUpdateTime(auth.getUpdateTime());
                item.setCreateUserName(auth.getCreateUserName());
                item.setUpdateUserName(auth.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(authDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemAuthCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
                || StringUtils.isBlank(param.getUrl())
                || StringUtils.isBlank(param.getAuthType())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicAuthCreateCloudParam authCreateCloudParam = new PublicAuthCreateCloudParam();
        authCreateCloudParam.setName(param.getName());
        authCreateCloudParam.setCode(param.getCode());
        authCreateCloudParam.setAuthType(param.getAuthType());
        authCreateCloudParam.setUrl(param.getUrl());
        authCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        authCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicAuthCloudService.create(authCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemAuthUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getAuthPrimaryKey())
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
                || StringUtils.isBlank(param.getUrl())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicAuthUpdateCloudParam authUpdateCloudParam = new PublicAuthUpdateCloudParam();
        authUpdateCloudParam.setAuthPrimaryKey(param.getAuthPrimaryKey());
        authUpdateCloudParam.setName(param.getName());
        authUpdateCloudParam.setCode(param.getCode());
        authUpdateCloudParam.setAuthType(param.getAuthType());
        authUpdateCloudParam.setUrl(param.getUrl());
        authUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        authUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicAuthCloudService.update(authUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemAuthDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getAuthPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicAuthDeleteCloudParam authDeleteCloudParam = new PublicAuthDeleteCloudParam();
        authDeleteCloudParam.setAuthPrimaryKeys(param.getAuthPrimaryKeys());
        authDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        authDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicAuthCloudService.delete(authDeleteCloudParam);
    }
}
